package p.ithorns.sample.influxdb.core;

/**
 * FluxUtil
 *
 * @author HtL
 * @date 2025/1/17 8:53
 * @since 1.0.0
 */
public class FluxUtil {

    private String flux;

    @Override
    public String toString() {
        return "FluxUtil{" + "flux='" + flux + "'" + '}';
    }

    private FluxUtil(Builder builder) {
        flux = builder.flux.toString();
    }

    public static class Builder {
        private StringBuilder flux;

        public FluxUtil build() {
            return new FluxUtil(this);
        }

        {
            flux = new StringBuilder("from(bucket: \"&bucket&\")" +
                    "|> range(start:&startTime&,stop:&endTime&)");
        }

        /**
         * 设置桶名
         *
         * @param bucket 桶
         * @return Builder
         */
        public Builder setBucket(String bucket) {
            String str = flux.toString();
            str = str.replaceFirst("&bucket&", bucket);
            flux = new StringBuilder(str);
            return this;
        }

        /**
         * 设置开始时间
         *
         * @param startTime -代表从现在往前数 +代表从现在往后数 xxx ms
         * @return Builder
         */
        public Builder setStartTime(long startTime) {
            String str = flux.toString();
            str = str.replaceFirst("&startTime&", String.valueOf(startTime));
            flux = new StringBuilder(str);
            return this;
        }

        /**
         * 设置结束时间
         *
         * @param endTime -代表从现在往前数 +代表从现在往后数 xxx ms
         * @return Builder
         */
        public Builder setEndTime(long endTime) {
            String str = flux.toString();
            str = str.replaceFirst("&endTime&", String.valueOf(endTime));
            flux = new StringBuilder(str);
            return this;
        }

        /**
         * 设置measurements
         *
         * @param measurements 表名集合
         * @return Builder
         */
        public Builder setMeasurement(String... measurements) {
            StringBuilder str = new StringBuilder("|> filter(fn: (r) =>  ");
            flux.append(str);
            for (int i = 0; i < measurements.length; i++) {
                if (i != measurements.length - 1) {
                    flux.append("r[\"_measurement\"] == \"").append(measurements[i]).append("\"").append(" or ");
                } else {
                    flux.append("r[\"_measurement\"] == \"").append(measurements[i]).append("\")");
                }
            }
            return this;
        }

        /**
         * 设置fields
         *
         * @param fields 参数
         * @return Builder
         */
        public Builder setFields(String... fields) {
            StringBuilder str = new StringBuilder("|> filter(fn: (r) =>  ");
            flux.append(str);
            for (int i = 0; i < fields.length; i++) {
                if (i != fields.length - 1) {
                    flux.append("r[\"_field\"] == \"").append(fields[i]).append("\"").append(" or ");
                } else {
                    flux.append("r[\"_field\"] == \"").append(fields[i]).append("\")");
                }
            }
            return this;
        }

        /**
         * 设置limit
         *
         * @param limit 限制条数 最多查出多少条数据
         * @return Builder
         */
        public Builder setLimit(long limit) {
            flux.append("|> limit(n: " + limit + ")");
            return this;
        }
    }

}